package com.kun;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.args.GeoUnit;
import redis.clients.jedis.params.GeoRadiusParam;
import redis.clients.jedis.params.GeoRadiusStoreParam;
import redis.clients.jedis.resps.GeoRadiusResponse;

import java.util.List;

//测试redis的geospatial特殊数据结构
public class Test06_Geospatial {

    @Test
    public void testGeospatial() {
        Jedis jedis = JedisUtils.getJedis();
        jedis.flushDB();

        //1. 添加地理位置
        jedis.geoadd("myGeospatial", 116.30, 39.90, "beijing");
        jedis.geoadd("myGeospatial", 121.47, 31.23, "shanghai");
        jedis.geoadd("myGeospatial", 106.54, 29.59, "guangzhou");
        jedis.geoadd("myGeospatial", 104.06, 30.67, "shenzhen");
        jedis.geoadd("myGeospatial", 114.05, 22.54, "hangzhou");

        //2. 查询key中name的经纬度
        System.out.println(jedis.geopos("myGeospatial", "beijing"));//116.30,39.90

        //3. 查询两元素之间的距离
        System.out.println(jedis.geodist("myGeospatial", "beijing", "shanghai", GeoUnit.KM));//1071.2775

        //4. 通过半径查询元素--坐标
        List<GeoRadiusResponse> list = jedis.georadius("myGeospatial", 116.30, 39.90, 4000, GeoUnit.KM,
                new GeoRadiusParam().withCoord().withDist().count(2).sortAscending());//通过半径查询，携带坐标、距离、数量、排序；
        System.out.println(list.size());

        //5. 通过半径查询元素--元素
        List<GeoRadiusResponse> list1 = jedis.georadiusByMember("myGeospatial", "beijing", 4000, GeoUnit.KM,
                new GeoRadiusParam().withCoord().withDist().count(2).sortAscending());//通过半径查询，携带坐标、距离、数量、排序；
        System.out.println(list1.size());

        //6. 返回元素名称的hash值
        System.out.println(jedis.geohash("myGeospatial", "beijing", "shanghai", "guangzhou"));//"wx4g0s000000", "wx4g0s000000", "wx4g0s000000"

        //7. 删除元素
        jedis.zrem("myGeospatial", "beijing", "shanghai", "guangzhou");
        System.out.println(jedis.zcard("myGeospatial"));

        JedisUtils.closeJedis(jedis);
    }


}
